
package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.BannerController;
import com.alimama.mobile.sdk.config.BannerController.BannerState;
import com.alimama.mobile.sdk.config.BannerController.OnStateChangeCallBackListener;
import com.alimama.mobile.sdk.config.BannerProperties;
import com.alimama.mobile.sdk.config.MmuController.ClickCallBackListener;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.xm9m.ibczy.mytest.R;

public class BannerActivity extends BaseActivity {
    private BannerProperties properties;
    private BannerController<?> mController;
	private MmuSDK mmuSDK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.banner);
        setTitle("横幅");

        ViewGroup nat = (ViewGroup) findViewById(R.id.bannerParent);
        String slotId = "59335";//注意：该广告位只做测试使用，请勿集成到发布版app中
//        String slotId = "63592";
        setupAlimama(nat, slotId);

        Button btRetry = (Button) findViewById(R.id.test);
        btRetry.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mController != null) {
                    mController.load();
                }
            }
        });

        Button btShow = (Button) findViewById(R.id.show);
        btShow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mController != null) {
                    mController.show();
                }
            }
        });

        Button btClose = (Button) findViewById(R.id.close);
        btClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mController != null) {
                    mController.close();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        boolean interrupt = false;
        if (mController != null) {// 通知Banner推广返回键按下，如果Banner进行了一些UI切换将返回true
            // 否则返回false(如从 expand状态切换会normal状态将返回true)
            interrupt = mController.onBackPressed();
        }

        if (!interrupt)
            super.onBackPressed();
    }

    private void setupAlimama(ViewGroup nat, String slotId) {
        mmuSDK = MmuSDKFactory.getMmuSDK();
        mmuSDK.init(getApplication());//初始化SDK,该方法必须保证在集成代码前调用，可移到程序入口处调用
        mmuSDK.accountServiceInit(this);
        properties = new BannerProperties(slotId, nat);
        properties.setStretch(false);
        mmuSDK.attach(properties);
        properties.setClickCallBackListener(new ClickCallBackListener() {

            @Override
            public void onClick() {
                Log.i("munion", "onclick");
            }
        });
        properties.setOnStateChangeCallBackListener(new OnStateChangeCallBackListener() {

            @Override
            public void onStateChanged(BannerState state) {
                Log.i("munion", "state = " + state);
            }
        });
        mController =  properties.getMmuController();
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (!mmuSDK.accountServiceHandleResult(requestCode, resultCode, data,this)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}
